<?php

namespace App\Http\Middleware;

use App\Common\Server;
use Closure;
use Illuminate\Support\Facades\Cache;

class CheckUser
{

    private $action = [
        'api/auth/register',
        'api/auth/login',
        'api/auth/code',
        'api/auth/logout',
        'api/auth/resetPassword',
        'api/auth/resetSecondaryPassword',
        'api/menu',
        'api/config',
        'api/uploadImage',
        'api/package',
        'api/index'
    ];
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    use Server;
    public function handle($request, Closure $next)
    {
        $url = $request->route()->uri();
        $token = $request->header('token');
        $user = Cache::store('redis')->get($token);
        if(!in_array($url,$this->action)){
            if(empty($user)){
                return $this->tokenError();
            }
        }

        /*$request->header('Access-Control-Allow-Origin', '*');
        $request->header("Access-Control-Allow-Headers","*");

        $request->header('Access-Control-Allow-Headers','DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization');

        $request->header('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');

        $request->header('Access-Control-Allow-Headers','Authorization,lpy');
        $request->header('Access-Control-Allow-Headers', 'X-Requested-With,X_Requested_With,X-PINGOTHER,Content-Type');
*/

        return $next($request);

    }
}
